<template>
    <rx-dialog @handOk="handleSubmit" @cancel="cancelFun">
        <rx-layout style="width: calc(100% - 2px)">
            <div slot="left" width="200" showButton="true">
                <rx-category-treeeditor
                    ref="categoryTree"
                    cat-key="BPM"
                    :statisticConfig="statisticConfig"
                    :read-key="readKey"
                    :edit="false"
                    @handSelect="handSelect"
                    :async="true"
                    v-if="isShowManageTree"
                    :isAdmin="isManageToTree"
                    :treeRootList="treeRootList"
                ></rx-category-treeeditor>
            </div>
            <div slot="header">
                <div class="table-page-search-wrapper">
                    <a-form layout="inline">
                        <a-row :gutter="24">
                            <a-col :span="8">
                                <a-form-item :label="i18n('processDef.name', '标题')">
                                    <a-input :placeholder="i18n('processDef.name', '标题','input')" v-model="queryParam.Q_NAME__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col  :span="8">
                                <a-form-item :label="i18n('processDef.key', '编码')">
                                    <a-input :placeholder="i18n('processDef.key', '编码','input')" v-model="queryParam.Q_KEY__S_LK"/>
                                </a-form-item>
                            </a-col>
                            <a-col :span="8">
                                <a-form-item  >
                                    <a-button type="primary" @click="$refs.table.loadData()" icon="search">{{$ti18('rxList.searchBtn', '查询')}}</a-button>
                                </a-form-item>
                            </a-col>
                        </a-row>
                    </a-form>
                </div>
            </div>
            <div slot="center">
                <rx-fit>
                    <rx-grid
                        v-if="isShowManageTree"
                        ref="table"
                        bordered
                        :allowRowSelect="true"
                        :multiSelect="true"
                        :columns="columns"
                        :defaultPageSize="10"
                        url="/api-bpm/bpm/core/bpmDef/query"
                        data-field="result.data"
                        :pageSizeOptions="['10','30','40']"
                        id-field="defId"
                        @selectChange="onSelectChange"
                        :queryParam="queryParam"
                    >
            <span slot="status" slot-scope="{text}">
              <a-tag
                  :color="text==='DEPLOYED' ? 'green' : 'red'">
                <span v-if="text==='DEPLOYED'">{{i18n('processDef.publish','发布')}}</span>
                <span v-else-if="text==='DRAFT'">{{i18n('processDef.draft','草稿')}}</span>
                <span v-else-if="text==='INVALID'">{{i18n('processDef.invalid','失效')}}</span>
              </a-tag>
            </span>
                    </rx-grid>
                </rx-fit>
            </div>
        </rx-layout>
    </rx-dialog>
</template>

<script>
    import {RxLayout, Util, BaseForm, Dialog,App} from '@lowcode/jpaas-common-lib'
    import RxCategoryTreeeditor from "@/components/redxun/rx-category-treeeditor.vue";
    import instAndTaskShare from "@/views/modules/bpm/core/js/instAndTaskShare";
    export default {
        name: "BpmDefDialog",
        mixins: [BaseForm,instAndTaskShare,App],
        components: {
            RxLayout,RxCategoryTreeeditor
        },
        data() {
            return {
                selectedData: [],
                //默认查询状态为发布的流程定义
                queryParam: {
                    Q_STATUS__S_EQ: "DEPLOYED",
                    Q_TREE_ID__S_EQ:"",
                    Q_NAME__S_LK:"",
                    Q_KEY__S_LK:"",
                },
                // 表头
                columns: [
                    {
                        title: this.$ti18('rxList.index', '序号'),
                        type: 'indexColumn',
                        dataIndex: 'serial',
                        scopedSlots: {customRender: 'serial'},
                        width: 80
                    },
                    {
                        title: this.i18n('processDef.name', '标题'),
                        dataIndex: 'name',
                        width: 140
                    },
                    {
                        title: this.i18n('processDef.key', '编码'),
                        dataIndex: 'key',
                        width: 130
                    },
                    {
                        title: this.i18n('form.status', '状态'),
                        dataIndex: 'status',
                        width: 120,
                        scopedSlots: {customRender: 'status'}
                    },
                    {
                        title: this.i18n('processDef.version', '版本号'),
                        dataIndex: 'version',
                        width: 120
                    },
                    {
                        title: this.$ti18('rxForm.createTime', '创建时间'),
                        dataIndex: 'createTime',
                        width: 140
                    }
                ],
                statisticConfig: {
                    type:"static_bpm",
                    table:"BPM_DEF",
                    field:"TREE_ID_",
                    whereConf:[{name:"IS_MAIN_",type:"string",dateFormat:"",op:"=",value:"YES"}]
                },
                readKey:"def.read",
                isShowManageTree:false,
                isManageToTree:false,
                isBpmClient:true,
                searchName:"",
                searchKey:"",
                //弹框选择，默认可以获取到所有应用的
                isRootAdmin:true
            }
        },
        created() {
            this.queryParam["isRootAdmin"]=this.isRootAdmin;
        },
        methods: {
            i18n(name, text, key) {
                return this.$ti18(name, text, "BpmAgent", key);
            },
            cancelFun(){
                Util.closeWindow(this,"cancel");
            },
            handleSubmit(e) {
                Util.closeWindow(this, "ok", this.selectedData);
            },

            onSelectChange(selectedRowKeys, selectedRows) {
                var selected = [];
                if (selectedRows.length > 0) {
                    for (var i = 0; i < selectedRows.length; i++) {
                        var onj = {
                            serial: i + 1,
                            defId: selectedRowKeys[i],
                            defName: selectedRows[i].name
                        }
                        selected.push(onj);
                    }
                    this.selectedData = selected;
                } else {
                    this.selectedData = [];
                }
            },
            loadData() {
                if(!this.isShowManageTree){
                    return;
                }
                this.$refs.table.loadData();
            },
            handSelect(curRow) {
                this.curTree = curRow;
                var treeId=curRow.treeId;
                let treeKey=this.getKeyVal(this.treePrefix,'TREE_ID__S_EQ');
                let appKey=this.getKeyVal(this.appPrefix,'APP_ID__S_EQ');
                var param={}
                this.queryParam[appKey]="";
                this.queryParam['treeCatType']="";
                this.queryParam['treeRootList']="";
                if (treeId!="0"){
                    param[treeKey]=curRow.treeId ;
                }else {
                    param[treeKey]='';
                    //顶级目录（包括平台与应用目录）
                    if("rootApp"!=curRow.treeCatType){
                        param['treeCatType']='allRootApp';
                        param['treeRootList']=JSON.stringify(this.getAppList());
                    }
                }
                if("rootApp"==curRow.treeCatType){
                    param = {
                        'treeCatType':'customApp',
                        'treeRootList':JSON.stringify(this.getAppList())
                    };
                    param[treeKey]='';

                    if("defaultTree"==curRow.appId){
                        //顶级平台目录
                        param['treeCatType']='defaultApp';
                    }else {
                        param[appKey]=curRow.appId;
                    }
                }
                this.loadDataByParam(param);
            },
            /**
             * 加载数据
             * @param parameter
             */
            loadDataByParam(parameter) {
                var params = parameter || {};
                let _queryParam = {};
                for (let key in this.queryParam) {//去掉前后空格；
                    if (typeof this.queryParam[key] == "string") {
                        _queryParam[key] = this.queryParam[key].trim();
                    }
                }
                this.queryParam = Object.assign(_queryParam, params);
                let appId=this.curApp.appId;
                if(this.hasAppId && appId){
                    this.queryParam.Q_APP_ID__S_EQ=appId;
                }
                this.$nextTick(() => {
                    this.$refs.table.loadData();
                })
            },

        }
    }
</script>

<style scoped>

</style>